var __spreadArray =
  (this && this.__spreadArray) ||
  function (to, from, pack) {
    if (pack || arguments.length === 2)
      for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
          if (!ar) ar = Array.prototype.slice.call(from, 0, i);
          ar[i] = from[i];
        }
      }
    return to.concat(ar || Array.prototype.slice.call(from));
  };
import React from 'react';
import { message } from 'antd';
import { wordsLimitValidator } from './wordsLimitValidator';
var Leggo = (function () {
  function Leggo(keyRef, setForceRender, schemaModel0, middleware, publicStates) {
    var _this = this;
    this.publicStates = {};
    this.allDisabledIsLockedToTrue = false;
    this.forceRenderMark = false;
    this.ref = keyRef;
    this.schemaModel = this.parseSchemaModel(schemaModel0, middleware);
    if (publicStates) {
      this.publicStates = publicStates;
    }
    this.forceLeggoFormRender = function () {
      _this.forceRenderMark = !_this.forceRenderMark;
      setForceRender(function (pre) {
        return pre + 1;
      });
    };
  }
  Leggo.createRules = function (rules, wordsLimit) {
    return wordsLimit
      ? __spreadArray(
          __spreadArray([], rules, true),
          [{ validator: wordsLimitValidator.bind(null, wordsLimit) }],
          false
        )
      : rules;
  };
  Leggo.createChildren = function (childrenNode) {
    if (!childrenNode) {
      return;
    }
    var childrenType = typeof childrenNode;
    if (childrenType === 'function') {
      var Node_1 = childrenNode;
      return React.createElement(Node_1, null);
    }
    if (childrenType === 'string') {
      return childrenNode;
    }
  };
  Leggo.prototype.parseSchemaModel = function (schemaModel0, middleware) {
    try {
      schemaModel0 === null || schemaModel0 === void 0
        ? void 0
        : schemaModel0.schemaList.forEach(function (schema, index) {
            schema.linkingStringedNames = new Set();
            schema.getStringedName = function () {
              return String(schema.configs.itemProps.name);
            };
            middleware && middleware(schema.configs, index);
          });
    } catch (e) {
      message.error('解析失败!');
      console.log(e);
    } finally {
      return schemaModel0;
    }
  };
  Leggo.prototype.resetSchemaModel = function (newSchemaModel0, middleware, publicStates) {
    this.schemaModel = this.parseSchemaModel(newSchemaModel0, middleware);
    if (publicStates) {
      this.publicStates = publicStates;
    }
    this.forceLeggoFormRender();
  };
  Leggo.prototype.updateSchema = function (formItemName, changeSchemaFunc) {
    var _a, _b;
    var targetSchema =
      (_a = this.schemaModel) === null || _a === void 0
        ? void 0
        : _a.schemaList.find(function (schema) {
            return schema.getStringedName() === String(formItemName);
          });
    if (targetSchema) {
      var configs = targetSchema.configs;
      changeSchemaFunc(configs);
      (_b = targetSchema.forceLeggoFormItemRender) === null || _b === void 0
        ? void 0
        : _b.call(targetSchema);
    }
  };
  Leggo.prototype.lockAllDisabledToTrue = function (status) {
    if (status === void 0) {
      status = true;
    }
    this.allDisabledIsLockedToTrue = status;
    this.schemaModel.schemaList.forEach(function (schema) {
      return (schema.configs.inputProps.disabled = status);
    });
    this.forceLeggoFormRender();
  };
  return Leggo;
})();
export { Leggo };
